/**
 * @file models/fundResult.js
 * @author
 */

import api from '../api';

export default {
	namespace: 'fundResult',
	state: {
		names:[],
		weight:[],
		rights:[],
		table:[],
		org:{}
	},
	subscriptions : {
		setup({dispatch, history}) {
			history.listen(({ pathname, query }) => {
				// 没有参数时，向后端请求数据，并且改变query
				if( pathname === '/fundAnalysis/custodianEvaluation'){
					dispatch({type: 'whetherRequest',payload:query})
				}
			})
		}
	},
	effects : {

		* whetherRequest({
				payload
			}, { put,select}) {
				yield put({type:'clearState'})
				const {
					lPortId,
					timeRange,
					// cBenchCode,
					beginDate,
					endDate
				}=payload
				if(lPortId||timeRange){
					if(lPortId&&((timeRange=='0'&&beginDate&&endDate)||(timeRange!='0'))){
						yield put({
							type:'query',
							payload
						})
					}
				}
		},

		* query({payload}, {call, put,select}){

			// const brinson = yield call(api.getfundReportList, payload);
			const brinson = {};
			brinson.data = [
				{reportName:'qwqw',url:'ssss'},
				{reportName:'rsss',url:'asss'}
			];
			if(brinson.data.length>0){
				const table=brinson.data;
				yield put({type:'clearState'})

				yield put({
					type: 'querySuccess',
					payload: {
						table,
					}
				});
			}

		},
	},
	reducers : {
		setState(state, {payload}) {
			return {
				...state,
				...payload
			}
		},

		clearState(state) {
      return{
				names:[],
				weight:[],
				rights:[],
				table:[],
				org:{}
			}
    },

		querySuccess(state, {payload}) {
			return {
				...state,
				...payload
			}
		}
	}
};
